$(document).ready(function () {
	$('body').height( $(window).height() );//设置body高度

	$(window).resize(function () {
		$('body').height( $(window).height() );//设置body高度
		amend_fluid_place();//修正登录框位置
	});

	//点击消券查询  跳转到消券页面
	$('.title').find('.pull-left').eq(0).on('click',function () {
		window.location = '/index/qxww_stamps_search'
	});

	//修正登录框位置
	amend_fluid_place();
	function amend_fluid_place(){
		var window_w = $(window).width();
		var window_h = $(window).height();
		var w = $('.container').width();
		var h = $('.container').height();
		$('.container').css('left', ( window_w - w ) / 2.1 );
		$('.container').css('top', ( window_h - h ) / 3 );
	}

	//券号验证 、 防伪码验证
	stamp_anticounterfeit_verify();
	function stamp_anticounterfeit_verify() {
		var exampleInputName2 = $('#exampleInputName2');//券号
		var exampleInputEmail2 = $('#exampleInputEmail2');//姓名/手机号

		var stamp_reg = new RegExp("^[0-9]{6}$");//券号验证
		var name_reg = /^[\u4E00-\u9FA5]{2,4}$/;//姓名/手机号  姓名验证
		var phone_reg = /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;//姓名/手机号 手机号验证

		var stamp_reg_bool = false;//券号
		var anticounterfeit_reg_bool = false;//防伪码

		//券号验证
		exampleInputName2.on('keyup',function () {
			if( stamp_reg.test( $(this).val() ) ){
				$('#error_tip').css('display','none');
				stamp_reg_bool = true;
			}else {
				$('#error_tip').text('券号输入错误');
				$('#error_tip').css('display','block');
				stamp_reg_bool = false;
			}

			style_switch_search_btn(stamp_reg_bool,anticounterfeit_reg_bool);//查询按钮  样式交互
		});

		//姓名/手机号验证
		exampleInputEmail2.on('keyup',function () {
			if( name_reg.test( $(this).val() ) || phone_reg.test( $(this).val() ) ){
				$('#error_tip').css('display','none');
				anticounterfeit_reg_bool = true;
			}else {
				$('#error_tip').text('姓名或手机号输入错误');
				$('#error_tip').css('display','block');
				anticounterfeit_reg_bool = false;
			}

			style_switch_search_btn(stamp_reg_bool,anticounterfeit_reg_bool);//查询按钮  样式交互
		});
	}

	//查询按钮  样式交互              券号           防伪码
	function style_switch_search_btn(stamp_reg_bool,anticounterfeit_reg_bool) {
		var search_btn = $('#search_btn');//查询按钮

		if( stamp_reg_bool && anticounterfeit_reg_bool ){
			search_btn.addClass('active');
		}else {
			search_btn.removeClass('active');
		}
	}

	//查询 功能
	search_btn_form_post();
	function search_btn_form_post() {
		var search_btn = $('#search_btn');//查询按钮

		search_btn.on('click',function () {
			if( $(this).hasClass('active') ){
				$('#stamp_form').submit();
			}
		});
	}

	//点击 他人修补  展示他人修补弹层
	click_other_repair();
	function click_other_repair() {
		var other_repair = $('#other_repair');
		other_repair.click(function () {
			amend_modal_place($('#stamp_modal_other'));//修正弹层位置
			$('#stamp_modal_other').modal({keyboard:false});
		});
	}

	//点击 本人修补  展示本人修补弹层
	click_self_repair();
	function click_self_repair() {
		var self_repair = $('#self_repair');
		self_repair.click(function () {
			amend_modal_place($('#stamp_modal'));//修正弹层位置
			$('#stamp_modal').modal({keyboard:false});
		});
	}

	//修正弹层位置
	function amend_modal_place($modal) {
		var window_width = $(window).width();
		var window_height = $(window).height();
		var width = $modal.width();
		var height = $modal.find('.modal-dialog').height();

		var left = ( window_width - width ) / 2;
		var top  = ( window_height - height ) / 4;

		$modal.css('left',left);
		$modal.css('top',top);
	}

	//他人修补  点击选择修补项目  展示下拉选择框
	click_show_select_div();
	function click_show_select_div() {
		var btn = $('#select_repair_project_input');//获取点击按钮
		var select = $('#select_repair_project_select');//获取下拉选择框
		
		btn.on('click',function () {
			var self = $(this);
			select.animate({
				height: 'toggle'
			},500,function () {
				var display = select.attr('style').split(' ')[1].split(';')[0];
				if(display == 'block'){
					btn.css('borderBottomLeftRadius','0');
					btn.css('borderBottomRightRadius','0');
					self.next().attr('src','/statics/images/sever_retrieve.png');
				}else {
					btn.css('borderBottomLeftRadius','5px');
					btn.css('borderBottomRightRadius','5px');
					self.next().attr('src','/statics/images/sever_unretrieve.png');
				}
			});
			btn.css('borderBottomLeftRadius','0');
			btn.css('borderBottomRightRadius','0');
		});
	}

	//他人修补  点击选择修补项目  展示下拉选择框   进行修补项目选择   多选
	click_select_repair_project();
	function click_select_repair_project() {
		var btn = $('#select_repair_project_input');//获取点击按钮
		var select = $('#select_repair_project_select');//获取下拉选择框
		select.delegate('div','click',function () {
			if( $(this).hasClass('active') ){
				$(this).removeClass('active');
			}else {
				$(this).addClass('active');
			}

			var active = select.find('.active');
			var arr_text = [];
			active.each(function (index,item) {
				arr_text.push( $(item).text() )
			});

			btn.val( arr_text.join('、') );
		});
	}

});